package com.caigou.controller;

import com.caigou.bean.cms.entity.LessonObject;
import com.caigou.bean.cms.entity.LessonSpan;
import com.caigou.bean.cms.param.*;
import com.caigou.bean.exam.entity.Users;
import com.caigou.enums.ResponseEnum;
import com.caigou.security.utils.CurrentUser;
import com.caigou.service.LessonService;
import com.caigou.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/lesson")
public class LessonController {
    @Resource
    private LessonService lessonService;

    @RequestMapping("listLessonSpan")
    @PreAuthorize("hasAnyAuthority('baomingjiangshiguanlichaxun')")
    public Result listLessonSpan(@CurrentUser Users user){
        List<LessonSpan> list = lessonService.listLessonSpan();
        return Result.success(list);
    }

    @RequestMapping("addLessonSpan")
    @PreAuthorize("hasAnyAuthority('baomingjiangshishezhi')")
    public Result addLessonSpan(@RequestBody LessonSpanParam param, @CurrentUser Users user){
        if(param.getSpanName()==null){
            return Result.error("政府采购从业时间的名称不能为空", "政府采购从业时间的名称不能为空");
        }
        if(param.getSpanName().length()>16){
            return Result.error("政府采购从业时间的名称不能超过6个字符", "政府采购从业时间的名称不能超过6个字符");
        }
        return lessonService.saveLessonSpan(param);
    }

    @RequestMapping("updateLessonSpan")
    @PreAuthorize("hasAnyAuthority('baomingjiangshishezhi')")
    public Result updateLessonSpan(@RequestBody LessonSpanParam param, @CurrentUser Users user){
        return lessonService.updateLessonSpan(param);
    }

    @RequestMapping("deleteLessonSpan")
    @PreAuthorize("hasAnyAuthority('baomingjiangshishezhi')")
    public Result deleteLessonSpan(@RequestBody LessonSpanParam param, @CurrentUser Users user){
        return lessonService.deleteLessonSpan(param.getId());
    }


    @RequestMapping("listLessonObject")
    @PreAuthorize("hasAnyAuthority('baomingjiangshiguanlichaxun')")
    public Result listLessonObject(@CurrentUser Users user){
        List<LessonObject> list = lessonService.listLessonObject();
        return Result.success(list);
    }

    @RequestMapping("addLessonObject")
    @PreAuthorize("hasAnyAuthority('baomingjiangshishezhi')")
    public Result addLessonObject(@RequestBody LessonObjectParam param, @CurrentUser Users user){
        if(param.getObjectName()==null){
            return Result.error("授课对象名称不能为空", "授课对象名称不能为空");
        }
        if(param.getObjectName().length()>16){
            return Result.error("授课对象不能超过16个字符", "授课对象名称不能超过16个字符");
        }
        return lessonService.saveLessonObject(param);
    }

    @RequestMapping("updateLessonObject")
    @PreAuthorize("hasAnyAuthority('baomingjiangshishezhi')")
    public Result updateLessonObject(@RequestBody LessonObjectParam param, @CurrentUser Users user){
        return lessonService.updateLessonObject(param);
    }

    @RequestMapping("deleteLessonObject")
    @PreAuthorize("hasAnyAuthority('baomingjiangshishezhi')")
    public Result deleteLessonObject(@RequestBody LessonObjectParam param, @CurrentUser Users user){
        return lessonService.deleteLessonObject(param.getId());
    }

    @RequestMapping("getLessonConfig")
    @PreAuthorize("hasAnyAuthority('baomingjiangshiguanlichaxun')")
    public Result getLessonConfig(@CurrentUser Users user){
        return lessonService.getLessonConfig();
    }

    @RequestMapping("saveLessonConfig")
    @PreAuthorize("hasAnyAuthority('baomingjiangshishezhi')")
    public Result saveLessonConfig(@RequestBody AttachmentConfigParam param, @CurrentUser Users user){
        return lessonService.saveLessonConfig(param);
    }

    @RequestMapping("queryLesson")
    @PreAuthorize("hasAnyAuthority('baomingjiangshiguanlichaxun')")
    public Result queryLesson(@RequestBody LessonQueryParam param, @CurrentUser Users user){
        if(param.getStartLessonTime()!=null){
            param.setStartLessonTime(param.getStartLessonTime()+" 00:00:00");
        }
        if(param.getEndLessonTime()!=null){
            param.setEndLessonTime(param.getEndLessonTime()+" 23:59:59");
        }
        if(param.getStartProcessTime()!=null){
            param.setStartProcessTime(param.getStartProcessTime()+" 00:00:00");
        }
        if(param.getEndProcessTime()!=null){
            param.setEndProcessTime(param.getEndProcessTime()+" 23:59:59");
        }
        return lessonService.queryLesson(param);
    }

    @RequestMapping("getLessonDetail")
    @PreAuthorize("hasAnyAuthority('baomingjiangshiguanlichaxun')")
    public Result getLessonDetail(@RequestBody LessonParam param, @CurrentUser Users user){
        return lessonService.getLessonDetail(param);
    }

    @RequestMapping("processLesson")
    @PreAuthorize("hasAnyAuthority('baomingjiangshibianji')")
    public Result processLesson(@RequestBody LessonParam param, @CurrentUser Users user){
        if(param.getProcessStatus().intValue()!=1&&param.getProcessStatus()!=2){
            return Result.error("请选择处理状态", "请选择处理状态");
        }
        if(param.getProcessNote()!=null&&param.getProcessNote().length()>150){
            return Result.error("处理方法备注不能超过150个字", "处理方法备注不能超过150个字");
        }
        return lessonService.processLesson(param, user.getUserid());
    }


}
